package com.kg.sdk.util;
import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;



import java.lang.reflect.Field;


public class ViewUtil {
    private static final String TEXTVIEW_CLASS = "android.widget.TextView";
    private static final String  IMAGEVIEW_CLASS = "android.widget.ImageView";
    private static final String  BUTTON_CLASS = "android.widget.Button";
    private static final String  LINEARLAYOUT_CLASS = "android.widget.LinearLayout";
    private static final String  FRAMELAYOUT_CLASS = "android.widget.FrameLayout";
    private static final String  RELATIVELAYOUT_CLASS = "android.widget.RelativeLayout";
    private static final String  VIEW_CLASS = "android.view.View";
    public static void getViewItem(Class<?> cls,Object obj, View parentView){
        getViewItem(cls,obj,parentView,null);
    }

    public static void getViewItem(Class<?> cls,Object obj, View parentView,View.OnClickListener listener){
        Field[] fields = cls.getDeclaredFields();
        if(fields != null) {
            for (Field field : fields) {
                field.setAccessible(true);
                String typeName = field.getType().getName();
                String name = field.getName();
                if(name == null){
                    continue;
                }
                int id = Helper.getIdResId(parentView.getContext(),parentView.getContext().getPackageName(),name);
                //LogUtil.e(" field id is "+id);
                if(id <= 0){
                    continue;
                }
                try {
                    View view = parentView.findViewById(id);
                    if(view != null){
                        field.set(obj,view);
                        if(listener != null){
                            view.setOnClickListener(listener);
                        }
                    }
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }

            }
        }
    }

}